using System;
using System.Windows.Forms;
using Accounting.Model;

namespace EIM_Accounting
{
    /// <summary>
    /// 共通 成员管理 成员编辑
    /// </summary>
    public partial class CM010102 : Form
    {
        public CM010102()
        {
            InitializeComponent();
        }

        private void CM010102_Load(object sender, System.EventArgs e)
        {
            comboBox1.Items.AddRange(new string[] { "男", "女" });
            cmbBank.Items.AddRange(new string[] { "交通银行", "招商银行", "工行", "农行", "建行" });
            cmbType.Items.AddRange(new string[] { "本人", "合作者", "正社员", "临社员", "试用", "其他" });
            comboBox1.SelectedIndex = 0;
            this.enterDate.Format = DateTimePickerFormat.Custom;
            this.outDate.Format = DateTimePickerFormat.Custom;

            if (staffId == null || staffId == string.Empty)
            {
                cmbBank.SelectedIndex = 0;
                cmbType.SelectedIndex = 0;
                //this.enterDate.Format = DateTimePickerFormat.Custom;
                this.enterDate.CustomFormat = "   ";
                this.outDate.CustomFormat = "   ";
            }
            else
            {
                EIM_Accounting.BLL.CM010102 cm010102 = new EIM_Accounting.BLL.CM010102();
                Staff staff = cm010102.getModel(int.Parse(staffId));
                cmbBank.SelectedItem = staff.CardBank;
                cmbType.SelectedItem = staff.StaffType;

                txtName.Text = staff.Name;
                txtAlies.Text = staff.Alies;

                txtCardNo.Text = staff.CardNo;
                txtCardName.Text = staff.CardName;

                txtRemark.Text = staff.Remark;

                if (staff.EnterDate == null || staff.EnterDate == string.Empty)
                {
                    this.enterDate.CustomFormat = "   ";
                }
                else
                {
                    this.enterDate.CustomFormat = "yyyy 年 MM 月 dd 日";
                    string dateStr = staff.EnterDate.Substring(0, 4) + "-" + staff.EnterDate.Substring(4, 2) + "-" + staff.EnterDate.Substring(6, 2);
                    this.enterDate.Value = DateTime.Parse(dateStr);
                }

                if (staff.OutDate == null || staff.OutDate == string.Empty)
                {
                    this.outDate.CustomFormat = "   ";
                }
                else
                {
                    this.outDate.CustomFormat = "yyyy 年 MM 月 dd 日";
                    string dateStr = staff.OutDate.Substring(0, 4) + "-" + staff.OutDate.Substring(4, 2) + "-" + staff.OutDate.Substring(6, 2);
                    this.outDate.Value = DateTime.Parse(dateStr);
                }
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, System.EventArgs e)
        {
            ((DateTimePicker)sender).CustomFormat = "yyyy 年 MM 月 dd 日";
        }

        private void dateTimePicker1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Back)
            {
                ((DateTimePicker)sender).CustomFormat = "   ";
            }
        }

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, System.EventArgs e)
        {
            string strErr = "";
            if (this.txtName.Text.Trim().Length == 0)
            {
                strErr += "Name不能为空！\\n";
            }
            if (this.txtAlies.Text.Trim().Length == 0)
            {
                strErr += "Alies不能为空！\\n";
            }
            Staff staff = new Staff();

            staff.Name = txtName.Text;
            staff.Alies = txtAlies.Text;
            staff.StaffType = cmbType.SelectedItem.ToString();

            staff.CardNo = txtCardNo.Text;
            staff.CardName = txtCardName.Text;
            staff.CardBank = cmbBank.SelectedItem.ToString();

            staff.Remark = txtRemark.Text;
            if (enterDate.CustomFormat == "   ") { staff.EnterDate = string.Empty; } else { staff.EnterDate = enterDate.Value.ToString("yyyyMMdd"); }
            if (outDate.CustomFormat == "   ") { staff.OutDate = string.Empty; } else { staff.OutDate = outDate.Value.ToString("yyyyMMdd"); }

            EIM_Accounting.BLL.CM010102 cm010102 = new EIM_Accounting.BLL.CM010102();
            if (staffId == null || staffId == string.Empty)
            {
                cm010102.Add(staff);
            }
            else
            {
                staff.ID = int.Parse(staffId);
                cm010102.Update(staff);
            }
        }

        public string staffId { get; set; }
    }
}